package com.ruoyi.qbms.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.BaseSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.qbms.mapper.QbmsSubjectMapper;
import com.ruoyi.qbms.domain.QbmsSubject;
import com.ruoyi.qbms.service.IQbmsSubjectService;

/**
 * 题库科目Service业务层处理
 * 
 * @author zouhuu
 * @date 2022-10-16
 */
@Service
public class QbmsSubjectServiceImpl implements IQbmsSubjectService 
{
    @Autowired
    private QbmsSubjectMapper qbmsSubjectMapper;

    /**
     * 查询题库科目
     * 
     * @param id 题库科目主键
     * @return 题库科目
     */
    @Override
    public QbmsSubject selectQbmsSubjectById(Long id)
    {
        return qbmsSubjectMapper.selectQbmsSubjectById(id);
    }

    /**
     * 查询学科选项
     * @param qbmsSubject
     * @return
     */
    @Override
    public List<QbmsSubject> selectQbmsSubjectOptions(QbmsSubject qbmsSubject) {
        return qbmsSubjectMapper.selectQbmsSubjectOptions(qbmsSubject);
    }

    /**
     * 查询学科树选项
     * @param qbmsSubject
     * @return
     */
    @Override
    public List<BaseSelect> selectQbmsSubjectTreeselect(QbmsSubject qbmsSubject) {
        // 查询科目列表数据
        List<QbmsSubject> qbmsSubjects = qbmsSubjectMapper.selectQbmsSubjectOptions(qbmsSubject);
        // 分组
        Map<String, List<QbmsSubject>> collect = qbmsSubjects.stream().collect(Collectors.groupingBy(QbmsSubject::getCategoryName));
        // 形成树结构
        ArrayList<BaseSelect> baseSelects = new ArrayList<>();
        collect.forEach((onekey, onevalue) -> {
            // 构建二级节点
            List<BaseSelect> twoSelets = onevalue.stream().map(item -> new BaseSelect(item.getId(), item.getName())).collect(Collectors.toList());
            BaseSelect oneSelect = new BaseSelect();
            oneSelect.setId(0L);
            oneSelect.setLabel(onekey);
            oneSelect.setChildren(twoSelets);
            baseSelects.add(oneSelect);
        });
        return baseSelects;
    }

    /**
     * 查询题库科目列表
     * 
     * @param qbmsSubject 题库科目
     * @return 题库科目
     */
    @Override
    public List<QbmsSubject> selectQbmsSubjectList(QbmsSubject qbmsSubject)
    {
        return qbmsSubjectMapper.selectQbmsSubjectList(qbmsSubject);
    }

    /**
     * 新增题库科目
     * 
     * @param qbmsSubject 题库科目
     * @return 结果
     */
    @Override
    public int insertQbmsSubject(QbmsSubject qbmsSubject)
    {
        qbmsSubject.setCreateTime(DateUtils.getNowDate());
        qbmsSubject.setCreateBy(SecurityUtils.getUsername());
        qbmsSubject.setUserId(SecurityUtils.getUserId());
        qbmsSubject.setDeptId(SecurityUtils.getUserId());
        return qbmsSubjectMapper.insertQbmsSubject(qbmsSubject);
    }

    /**
     * 修改题库科目
     * 
     * @param qbmsSubject 题库科目
     * @return 结果
     */
    @Override
    public int updateQbmsSubject(QbmsSubject qbmsSubject)
    {
        qbmsSubject.setUpdateTime(DateUtils.getNowDate());
        qbmsSubject.setUpdateBy(SecurityUtils.getUsername());
        return qbmsSubjectMapper.updateQbmsSubject(qbmsSubject);
    }

    /**
     * 批量删除题库科目
     * 
     * @param ids 需要删除的题库科目主键
     * @return 结果
     */
    @Override
    public int deleteQbmsSubjectByIds(Long[] ids)
    {
        return qbmsSubjectMapper.deleteQbmsSubjectByIds(ids);
    }

    /**
     * 删除题库科目信息
     * 
     * @param id 题库科目主键
     * @return 结果
     */
    @Override
    public int deleteQbmsSubjectById(Long id)
    {
        return qbmsSubjectMapper.deleteQbmsSubjectById(id);
    }
}
